package jp.bsws.action.master;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import jp.bsws.bean.KeyValueBean;
import jp.bsws.bean.ShiharaiHohoBean;
import jp.bsws.bean.SyohizeiBean;
import jp.bsws.common.BSWSConstants;
import jp.bsws.common.BSWSSession;
import jp.bsws.common.BSWSUtils;
import jp.bsws.common.DBUtils;
import jp.bsws.dao.BSWSDao;
import jp.bsws.dao.SyohizeiDao;
import jp.bsws.model.SyohizeiModel;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/*
 * メインメニューアクションクラス
 */
public class SyohizeiMasterAction extends ActionSupport {
	private static final long serialVersionUID = 1L;

	private BSWSSession bSWSSession;

	public String getLoginJugyoinKnj() { return bSWSSession.getStrJugyoinKnj(); }

	private List<SyohizeiBean> syohizeiBeanList;

	private Integer intTaisyoSeq;

	public void validateUpdateSyohizei() {

		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);

		//必須チェック
		for(int i =0; i < syohizeiBeanList.size(); i++){
			SyohizeiBean syohizeiBean = syohizeiBeanList.get(i);
			if(intTaisyoSeq == syohizeiBean.getSeq() && syohizeiBean.isChkEdit()){
				if (BSWSUtils.isBlankNull(syohizeiBean.getStrSyohizeiNum())) {
					addFieldError("syohizeiBeanList[" + i + "].strSyohizeiNum", (i + 1) + "行目の消費税を入力して下さい。");
				}
				break;
			}
		}
	}

	private void init() throws Exception{
		//セッション情報を取得
		HttpSession session = ServletActionContext.getRequest().getSession();
		bSWSSession = (BSWSSession) session.getAttribute(BSWSConstants.KEY_SESSION);
	}

	@Override
	public String execute() throws Exception{

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//画面表示内容の設定
			hyojiJunbi(con);
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	/**
	 * 消費税追加
	 * @return SUCCESS
	 * @throws Exception
	 */
	public String add() throws Exception{

		init();

		for(SyohizeiBean syohizeiBean : this.syohizeiBeanList) {
			syohizeiBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			syohizeiBean.setStrTenpoCod(bSWSSession.getStrSansyoTenpoCod());
			syohizeiBean.setTenpoList(bSWSSession.getSansyoTenpoList());
		}

		//消費税行追加
		SyohizeiBean syohizeiBean = new SyohizeiBean();
		syohizeiBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		syohizeiBean.setStrTenpoCod(bSWSSession.getStrSansyoTenpoCod());
		syohizeiBean.setTenpoList(bSWSSession.getSansyoTenpoList());
		this.syohizeiBeanList.add(syohizeiBean);

		return SUCCESS;
	}

	public String updateSyohizei() throws Exception{

		SyohizeiDao syohizeiDao = SyohizeiDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{

			//消費税情報更新対象取得
			for(SyohizeiBean syohizeiBean : syohizeiBeanList) {
				int cnt = 0;
				if(syohizeiBean.getSeq() != null){
					//消費税情報更新
					cnt = syohizeiDao.updateSyohizei(con, bSWSSession, syohizeiBean);
				}else{
					if(BSWSUtils.isNotBlankNull(syohizeiBean.getStrSyohizeiNum())
							&& BSWSUtils.isNotBlankNull(syohizeiBean.getStrKaishiYmd())
							&& BSWSUtils.isNotBlankNull(syohizeiBean.getStrSyuryoYmd())
					){
						//消費税情報追加
						cnt = syohizeiDao.insertSyohizei(con, null, bSWSSession, syohizeiBean);
					}
				}
				if(cnt == 0){
					//処理なし
				}
			}
			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	public String deleteSyohizei() throws Exception{

		BSWSDao bSWSDao = BSWSDao.getInstance();

		//初期設定
		init();

		Connection con = DBUtils.getConnection();
		try{
			//消費税削除
			int cnt = bSWSDao.deleteRow(con, intTaisyoSeq, " M_SYOHIZEI ");
			if(cnt == 0){
				//処理なし
			}

			//画面表示内容の設定
			hyojiJunbi(con);

			con.commit();
		}catch(SQLException se){
			con.rollback();
			throw se;
		}finally{
			con.close();
		}

		return SUCCESS;
	}

	private void hyojiJunbi(Connection con) throws Exception{
		//消費税マスタ情報を取得
		getSyohizeiJoho(con);

		//消費税行追加
		SyohizeiBean syohizeiBean = new SyohizeiBean();
		syohizeiBean.setStrGroupCod(bSWSSession.getStrGroupCod());
		syohizeiBeanList.add(syohizeiBean);
	}

	private void getSyohizeiJoho(Connection con) throws Exception{

		SyohizeiDao syohizeiDao = SyohizeiDao.getInstance();
		this.syohizeiBeanList = new ArrayList<SyohizeiBean>();
		this.syohizeiBeanList.clear();
		//消費税マスタ情報を取得
		List<SyohizeiModel> syohizeiList = syohizeiDao.getSyohizeiJoho(con, null, bSWSSession.getStrGroupCod(), null);
		for(int i = 0; i < syohizeiList.size(); i++){
			SyohizeiBean syohizeiBean = new SyohizeiBean();
			syohizeiBean.setSeq(syohizeiList.get(i).getSeq());
			syohizeiBean.setStrGroupCod(bSWSSession.getStrGroupCod());
			Integer tmpSyohizei = syohizeiList.get(i).getBdSyohizeiNum().multiply(new BigDecimal(100)).intValue() ;
			syohizeiBean.setStrSyohizeiNum(String.valueOf(tmpSyohizei));
			syohizeiBean.setStrKaishiYmd(String.valueOf(syohizeiList.get(i).getIntKaishiYmd()));
			syohizeiBean.setStrSyuryoYmd(String.valueOf(syohizeiList.get(i).getIntSyuryoYmd()));
			this.syohizeiBeanList.add(syohizeiBean);
		}
	}

	public BSWSSession getBSWSSession() {
		return bSWSSession;
	}

	public void setBSWSSession(BSWSSession bSWSSession) {
		this.bSWSSession = bSWSSession;
	}

	public List<SyohizeiBean> getSyohizeiBeanList() {
		return syohizeiBeanList;
	}

	public void setSyohizeiBeanList(List<SyohizeiBean> syohizeiBeanList) {
		this.syohizeiBeanList = syohizeiBeanList;
	}

	public Integer getIntTaisyoSeq() {
		return intTaisyoSeq;
	}

	public void setIntTaisyoSeq(Integer intTaisyoSeq) {
		this.intTaisyoSeq = intTaisyoSeq;
	}
}